package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.repository.GuardrailFaultRepository;
import com.hualude.roadsign.repository.UserRepository;
import com.hualude.roadsign.service.GuardrailFaultHandleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by ding on 2017/8/15.
 */
@Service
public class GuardrailFaultHandleServiceImpl implements GuardrailFaultHandleService {
    @Resource
    private GuardrailFaultRepository guardrailFaultRepository;
    @Resource
    private UserRepository userRepository;

    public DTResponseDto getGuardrailFaultHandle4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer maintenanceStatus) throws Exception {
        List<DTGuardrailFaultDto> dto4DTs = guardrailFaultRepository.getGuardrailFaultHandle4DT(dtRequestDto, actorIds, orgId, maintenanceStatus);
        Integer count = guardrailFaultRepository.getGuardrailFaultHandleCount4DT(dtRequestDto, actorIds, orgId, maintenanceStatus);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        for (DTGuardrailFaultDto dtMachineFaultDto : dto4DTs) {
            if (dtMachineFaultDto.getGuardrailMaintainerId() != null) {
                String maintainerName = userRepository.getUserNameByUserId(dtMachineFaultDto.getGuardrailMaintainerId());
                dtMachineFaultDto.setGuardrailMaintainerName(maintainerName);
            }
        }
        return dtResponseDto;
    }

    @Transactional
    public Integer addGuardrailMaintenanceContent(Integer lampFaultId, String lampMaintenanceContent) throws Exception {
        return guardrailFaultRepository.addGuardrailMaintenanceContent(lampFaultId, lampMaintenanceContent);
    }

    @Transactional
    public Integer finishGuardrailMaintenance(String monitorMaintenancePhoto, Integer monitorMaintainerId, String monitorMaintenanceTime, String monitorMaintenanceExplain, Integer monitorFaultId) throws Exception {
        return guardrailFaultRepository.finishGuardrailMaintenance(monitorMaintenancePhoto, monitorMaintainerId, monitorMaintenanceTime, monitorMaintenanceExplain, monitorFaultId);
    }

    public List<GuardrailFaultDto> getGuardrailFaultHandleList4App(Integer orgId, Integer maintenanceStatus, String actorIds, Integer count, Integer size) throws Exception {
        return guardrailFaultRepository.getGuardrailFaultHandleList4App(orgId, maintenanceStatus, actorIds, count, size);
    }

    public GuardrailFaultDto getGuardrailReceiptInfo4App(Integer faultId) throws Exception {
        GuardrailFaultDto result = guardrailFaultRepository.getGuardrailReceiptInfo4App(faultId);
        GuardrailFaultDto latlngDto = guardrailFaultRepository.getGuardrailLatLng(result.getGuardrailId());
        result.setLongitude(latlngDto.getLongitude());
        result.setLatitude(latlngDto.getLatitude());
        return result;
    }
}
